package com.yxq.mdt.app.modules.bottomsheet;

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import androidx.annotation.Nullable;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.blankj.utilcode.util.ToastUtils;
import com.google.android.material.bottomsheet.BottomSheetDialogFragment;
import com.yxq.mdt.app.R;
import com.yxq.mdt.app.modules.order.bean.OrderConfirmBean;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.Unbinder;

/**
 * @author L
 * @time 2019-10-30
 * @describe 确认订单-优惠券列表
 */
public class OrderConfirmCouponListBottomSheetDialogFragment extends BottomSheetDialogFragment {

    public OrderConfirmCouponListBottomSheetDialogFragment(List<OrderConfirmBean.MchListBean.CouponListBean> mchListBean, OrderConfirmBean.MchListBean.CouponListBean couponListBean) {
        this.mchListBean = mchListBean;
        this.couponListBeanChecked = couponListBean;
    }

    Unbinder unbinder;
    @BindView(R.id.order_confirm_tv_goods_count)
    TextView orderConfirmTvGoodsCount;
    @BindView(R.id.recyclerView)
    RecyclerView recyclerView;
    List<OrderConfirmBean.MchListBean.CouponListBean> mchListBean;
    CouponListAdapter couponListAdapter;
    CouponCheckedInterface couponCheckedInterface;
    OrderConfirmBean.MchListBean.CouponListBean couponListBeanChecked;

    public void setCouponCheckedInterface(CouponCheckedInterface couponCheckedInterface) {
        this.couponCheckedInterface = couponCheckedInterface;
    }

    public interface CouponCheckedInterface {
        void checked(OrderConfirmBean.MchListBean.CouponListBean couponListBean);
    }

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        //设置背景透明，才能显示出layout中诸如圆角的布局，否则会有白色底（框）
        setStyle(BottomSheetDialogFragment.STYLE_NORMAL, R.style.CustomBottomSheetDialogTheme);
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.layout_order_confirm_counpon_bottom_sheet_fragment_dialog, container, false);
        unbinder = ButterKnife.bind(this, view);
        initViews(view);
        return view;
    }

    private void initViews(View view) {
        view.findViewById(R.id.goods_attr_iv_close).setOnClickListener(view1 -> dismiss());

    }

    public void setData() {
        if (mchListBean == null) {
            return;
        }
        orderConfirmTvGoodsCount.setText(String.format("共%d张", mchListBean.size()));
        recyclerView.setLayoutManager(new LinearLayoutManager(getContext()));
        couponListAdapter = new CouponListAdapter(R.layout.layout_coupon_item_layout, mchListBean, couponListBeanChecked);
        recyclerView.setAdapter(couponListAdapter);
        couponListAdapter.setCartCheckedInterface(position -> {
            if (couponCheckedInterface != null) {
//                couponCheckedInterface.checked(couponListAdapter.getItem(position));
//                dismiss();
            }
        });
        couponListAdapter.setOnItemClickListener((adapter, view, position) -> {
//            if (mchListBean.getTotal_price() < couponListAdapter.getItem(position).getMin_price()) {
//                ToastUtils.showShort(couponListAdapter.getItem(position).getMin_price_desc());
//                return;
//            }
            if (couponCheckedInterface != null) {
                couponCheckedInterface.checked(couponListAdapter.getItem(position));
                dismiss();
            }
        });
    }

    @Override
    public void onStart() {
        super.onStart();
        setData();
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();

        if (unbinder != null) {
            unbinder.unbind();
        }
    }
}
